package quests.Q404_PathToAHumanWizard;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;
import net.sf.l2j.gameserver.network.serverpackets.SocialAction;

/**
 * @author alexa026
 */
public class Q404_PathToAHumanWizard extends Quest
{
	private static final String qn = "Q404_PathToAHumanWizard";

	//Reward
	private static final int BeadOfSeason = 1292;
	
	//Items
	private static final int MapOfLuster = 1280;
	private static final int KeyOfFlame = 1281;
	private static final int FlameEaring = 1282;
	private static final int Mirror = 1283;
	private static final int WindFeather = 1284;
	private static final int WindBangel = 1285;
	private static final int RamasDiary = 1286;
	private static final int SparklePebble = 1287;
	private static final int WaterNecklace = 1288;
	private static final int GoldCoin = 1289;
	private static final int RedSoil = 1290;
	private static final int EarthRing = 1291;
	
	//NPCs
	private static final int Parina = 30391;
	private static final int EarthSnake = 30409;
	private static final int Lizardman = 30410;
	private static final int FireSalamander = 30411;
	private static final int WindSylph = 30412;
	private static final int WaterUndine = 30413;
	
	public Q404_PathToAHumanWizard(int questId, String name, String descr)
	{
		super(questId, name, descr);
		questItemIds = new int[] { MapOfLuster, KeyOfFlame, FlameEaring, Mirror, WindFeather, WindBangel, RamasDiary, SparklePebble, WaterNecklace, GoldCoin, RedSoil, EarthRing };
		
		addStartNpc(Parina);
		addTalkId(Parina, EarthSnake, Lizardman, FireSalamander, WindSylph, WaterUndine);
		
		addKillId(20021, 20359, 27030);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("30391-01.htm"))
		{
			if (player.getClassId().getId() == 0x0a)
			{
				if (player.getLevel() >= 19)
				{
					if (st.getQuestItemsCount(BeadOfSeason) == 1)
						htmltext = "30391-06.htm";
					else
						htmltext = "30391-04.htm";
				}
				else
					htmltext = "30391-03.htm";
			}
			else if (player.getClassId().getId() == 0x0b)
				htmltext = "30391-02a.htm";
			else
				htmltext = "30391-04.htm";
		}
		
		else if (event.equalsIgnoreCase("30391-02.htm"))
		{
			st.setState(State.STARTED);
			st.set("cond", "1");
			st.playSound("ItemSound.quest_accept");
		}

		else if (event.equalsIgnoreCase("30410-03.htm"))
		{
			st.set("cond", "6");
			st.playSound("ItemSound.quest_middle");
			st.takeItems(Mirror, 1);
			st.giveItems(WindFeather, 1);
		}

		else if (event.equalsIgnoreCase("30413-03.htm"))
		{
			st.set("cond", "10");
			st.playSound("ItemSound.quest_middle");
			st.takeItems(SparklePebble, 1);
			st.giveItems(WaterNecklace, 1);
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		int cond = st.getInt("cond");
		switch (st.getState())
		{
			case State.CREATED:
				htmltext = "30391-01.htm";
				break;
				
			case State.STARTED:
				switch (npc.getNpcId())
				{
					case Parina:
						if (cond == 1)
							htmltext = "30391-05.htm";
						else if (cond == 13)
						{
							htmltext = "30391-07.htm";
							st.takeItems(FlameEaring, 1);
							st.takeItems(WindBangel, 1);
							st.takeItems(WaterNecklace, 1);
							st.takeItems(EarthRing, 1);
							st.giveItems(BeadOfSeason, 1);
							st.addExpAndSp(3200, 1500);
							player.broadcastPacket(new SocialAction(player, 3));

							st.playSound("ItemSound.quest_finish");
							st.exitQuest(false);
							st.saveGlobalQuestVar("1ClassQuestFinished", "1");
						}
						else 
							htmltext = "30391-05.htm";
						break;
					case FireSalamander:
						if (cond == 1)
						{
							htmltext = "30411-01.htm";
							st.set("cond", "2");
							st.giveItems(MapOfLuster, 1);
						}
						else if (cond == 2)
							htmltext = "30411-02.htm";
						else if (cond == 3 && st.getQuestItemsCount(KeyOfFlame) == 1)
						{
							htmltext = "30411-03.htm";
							st.set("cond", "4");
							st.playSound("ItemSound.quest_middle");
							st.takeItems(MapOfLuster, 1);
							st.takeItems(KeyOfFlame, 1);
							st.giveItems(FlameEaring, 1);
						}
						else
							htmltext = "30411-04.htm";
						break;
					case WindSylph:
						if (cond == 4)		
						{
							htmltext = "30412-01.htm";
							st.set("cond", "5");
							st.playSound("ItemSound.quest_middle");
							st.giveItems(Mirror, 1);
						}
						else if (cond == 5)
							htmltext = "30412-02.htm";
						else if (cond == 6)
						{
							htmltext = "30412-03.htm";
							st.set("cond", "7");
							st.playSound("ItemSound.quest_middle");
							st.takeItems(WindFeather, 1);
							st.giveItems(WindBangel, 1);
						}
						else 
							htmltext = "30412-04.htm";
						break;
					case Lizardman:
						if (cond == 5)
							htmltext = "30410-01.htm";
						break;
					case WaterUndine:
						if (cond == 7)
						{
							st.set("cond", "8");
							htmltext = "30413-01.htm";
							st.giveItems(RamasDiary, 1);
						}
						else if (cond == 8)
							htmltext = "30413-02.htm";
						else if (cond == 9)
						{
							if (st.getQuestItemsCount(SparklePebble) == 2)
								htmltext = "30413-03.htm";
						}
						else 
							htmltext = "30413-04.htm";
						break;
					case EarthSnake:
						if (cond == 10)
						{
							htmltext = "30409-01.htm";
							st.set("cond", "11");
							st.playSound("ItemSound.quest_middle");
							st.giveItems(GoldCoin, 1);
						}
						else if (cond == 11)
							htmltext = "30409-02.htm";
						else if (cond == 12)
						{
							if (st.getQuestItemsCount(RedSoil) == 1)
							{
								htmltext = "30409-03.htm";
								st.set("cond", "13");
								st.playSound("ItemSound.quest_middle");
								st.takeItems(RedSoil, 1);
								st.giveItems(EarthRing, 1);
							}
						}
						break;
						
				}
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return null;
		
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
	
		if (cond == 2)
		{
			if (npcId == 20359)
			{	
				if (st.getQuestItemsCount(KeyOfFlame) == 0)
					
				{
					st.giveItems(KeyOfFlame, 1);
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "3");
				}
			}
		}
		else if (cond == 8)
		{
			if (npcId == 27030)
			{
				if (st.getQuestItemsCount(SparklePebble) < 2)
				{
					st.giveItems(SparklePebble, 1);
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "9");
				}
			}
		}
		else if (cond == 11)
		{
			if (npcId == 20021)
			{
				if (st.getQuestItemsCount(RedSoil) == 0)
				{
					st.giveItems(RedSoil, 1);
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "12");	
				}
			}
		}
	return null;
	}
	public static void main(String[] args)
	{
		new Q404_PathToAHumanWizard(404, qn, "Path to a Human Wizard");
	}

}